package com.zhiche.lisa.bms.controller.pay;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.dao.model.bill.Bill;
import com.zhiche.lisa.bms.dao.model.pay.PayApply;
import com.zhiche.lisa.bms.dao.model.price.PriceIncoming;
import com.zhiche.lisa.bms.pojo.vo.common.CommonVO;
import com.zhiche.lisa.bms.pojo.vo.pay.PayApplyCapitalVO;
import com.zhiche.lisa.bms.pojo.vo.pay.PayApplyObjVO;
import com.zhiche.lisa.bms.pojo.vo.pay.PayApplyVO;
import com.zhiche.lisa.bms.service.pay.PayApplyService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 支付申请单主表 前端控制器
 * </p>
 *
 * @author WangShuiJin
 * @since 2018-09-27
 */
@RequestMapping("/payApply")
@Api(value = "/payApply", description = "支付申请接口", tags = {"支付申请接口"})
@RestController
public class PayApplyController {
    private static final Logger LOGGER = LoggerFactory.getLogger(PayApplyController.class);

    @Autowired
    private PayApplyService payApplyService;

    @PostMapping(value = "/queryPayApplyPage")
    @ApiOperation(value = "查询支付申请列表（分页）", notes = "查询支付申请列表（分页）", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<PayApply>> queryPayApplyPage(@RequestBody Page<PayApply> page) {
        RestfulResponse<Page<PayApply>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Page<PayApply> resultData = payApplyService.queryPayApplyPage(page);
            result.setData(resultData);
        } catch (BaseException e) {
            LOGGER.error("/BillIoController.queryPayApplyPage ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillIoController.queryPayApplyPage ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/insertPayApply")
    @ApiOperation(value = "创建支付申请列表", notes = "创建支付申请列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<PayApply> insertPayApply(@RequestBody PayApplyVO payApplyVO) {
        RestfulResponse<PayApply> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            PayApply resultData = payApplyService.insertPayApply(payApplyVO);
            result.setData(resultData);
        } catch (BaseException e) {
            LOGGER.error("/BillIoController.insertPayApply ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillIoController.insertPayApply ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/updatePayApplyToDetailed")
    @ApiOperation(value = "修改支付申请，对应的关系", notes = "修改支付申请，对应的关系", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<PayApply> updatePayApplyToDetailed(@RequestBody PayApplyVO payApplyVO) {
        RestfulResponse<PayApply> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            payApplyService.updatePayApplyToDetailed(payApplyVO);
        } catch (BaseException e) {
            LOGGER.error("/BillIoController.updatePayApplyToDetailed ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillIoController.updatePayApplyToDetailed ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/getPayApplyByPayIdList")
    @ApiOperation(value = "根据支付申请ID，获取支付申请下的所有明细", notes = "根据支付申请ID，获取支付申请下的所有明细", httpMethod = "GET")
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<PayApplyObjVO> getPayApplyByPayIdList(@RequestParam long payId) {
        RestfulResponse<PayApplyObjVO> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            PayApplyObjVO resultData = payApplyService.getPayApplyByPayIdList(payId);
            result.setData(resultData);
        } catch (BaseException e) {
            LOGGER.error("/BillIoController.getPayApplyByPayIdList ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillIoController.getPayApplyByPayIdList ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/queryPayApplyByIds")
    @ApiOperation(value = "根据主键ID，查询支付申请列表（不分页）", notes = "根据主键ID，查询支付申请列表（不分页）", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<PayApply>> queryPayApplyByIds(@RequestBody List<Long> ids) {
        RestfulResponse<List<PayApply>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(payApplyService.queryPayApplyByIds(ids));
        } catch (BaseException e) {
            LOGGER.error("/PayApplyController.queryPayApplyByIds ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/PayApplyController.queryPayApplyByIds ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/commitConfirmPayApply")
    @ApiOperation(value = "支付申请，提交审核", notes = "支付申请，审核", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> commitConfirmPayApply(@RequestBody List<Long> payIds) {
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            // 提交审核
            payApplyService.commitConfirmPayApply(payIds);
        } catch (BaseException e) {
            LOGGER.error("/PayApplyController.verifyPayApply ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/PayApplyController.verifyPayApply ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/verifyPayApply")
    @ApiOperation(value = "支付申请，审核", notes = "支付申请，审核", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> verifyPayApply(@RequestBody List<Long> payIds) {
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            payApplyService.verifyPayApply(payIds);
        } catch (BaseException e) {
            LOGGER.error("/PayApplyController.verifyPayApply ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/PayApplyController.verifyPayApply ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/cancelVerifyPayApply")
    @ApiOperation(value = "支付申请，取消审核", notes = "支付申请，取消审核", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> cancelVerifyPayApply(@RequestBody List<Long> payIds) {
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            payApplyService.cancelVerifyPayApply(payIds);
        } catch (BaseException e) {
            LOGGER.error("/PayApplyController.cancelVerifyPayApply ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/PayApplyController.cancelVerifyPayApply ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/selectPayApplyListByAllMoney")
    @ApiOperation(value = "根据价格信息进行数据列绑定", notes = "绑定审核单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PayApplyCapitalVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<CommonVO<PayApplyCapitalVO>> selectPayApplyListByAllMoney(@RequestBody Map<String, Object> condition) {
        RestfulResponse<CommonVO<PayApplyCapitalVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(payApplyService.selectPayApplyListByAllMoney(condition));
        } catch (BaseException e) {
            LOGGER.error("/PayApplyController.selectPayApplyListByAllMoney ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/PayApplyController.selectPayApplyListByAllMoney ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/updateBankData")
    @ApiOperation(value = "更新银行信息", notes = "更新银行信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PayApplyCapitalVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<PayApply>> updateBankData(@RequestBody List<Long> payId, @RequestHeader("Authorization") String token) {
        RestfulResponse<List<PayApply>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(payApplyService.updateBankDataByPayIds(payId, token));
        } catch (BaseException e) {
            LOGGER.error("/PayApplyController.selectPayApplyListByAllMoney ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/PayApplyController.selectPayApplyListByAllMoney ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/verifyBillAndPayApply")
    @ApiOperation(value = "校验账单下的应付账款是否有过支付申请", notes = "校验账单下的应付账款是否有过支付申请", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PayApplyCapitalVO.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Object> verifyBillAndPayApply(@RequestBody Bill bill) {
        RestfulResponse<Object> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            if (Objects.isNull(bill) || Objects.isNull(bill.getId())) {
                result.setCode(CommonEnum.ERROR.getCode());
                result.setMessage("找不到参数");
                return result;
            }
            payApplyService.verifyBillAndPayApply(bill.getId());
        } catch (BaseException e) {
            LOGGER.error("PayApplyController.verifyBillAndPayApply ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("PayApplyController.verifyBillAndPayApply ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/payApplyToBms")
    public RestfulResponse<Object> payApplyToBms(@RequestBody PayApply payApply) {
        RestfulResponse<Object> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            PayApply payApplyById = payApplyService.selectById(payApply.getId());
            payApplyService.payApplyToBmsCreateBill(payApplyById);
        } catch (BaseException e) {
            LOGGER.error("PayApplyController.payApplyToBms ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("PayApplyController.payApplyToBms ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }
}

